<template>
  <div>
    <FormPanel
      ref="formPanel"
      :list="formList"
      :cols="1"
      :formType="type"
      :isSubmitBtn="false"
      @formChange="formChangeHandle"
    />
    <BaseDialog :visible.sync="visbleVehicle" title="车辆选择"
                :containerStyle="{ overflow: 'auto', paddingBottom: '60px' }"
                destroyOnClose>
      <add-vehicle
        selectionType="single"
        @selectArr="selectVehicleFun"
        @closeDialog='visbleVehicle = false'></add-vehicle>
    </BaseDialog>

    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="saveDialog" :loading="saveLoading" type="primary">保 存</el-button>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
  import { dictionary } from '@/utils/dictMixin';
  import addVehicle from '@/pages/customerService/SecurityCode/getSecurityCode/Vehicle';
  import {setSecurityCode,updateSecurityCode,getSecurityCodeByVin} from '@/api/customerService/SecurityCode/securityCodeUpdate/index';
  export default {
    components: {
      addVehicle
    },
    name: "addSecurityCode",
    mixins: [dictionary],
    props: {
      type: {
        type: String,
        default: 'add',
      },
      rowData: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      this.saveData = null;
      return {
        formList: this.createFormList(),
        fetchApi: null,
        saveLoading: false,
        visbleVehicle: false,
        modelId: '',
        bsckid: '',
        fetchParams: null,
      };
    },
    created() {
      this.init();
    },
    methods: {
      init() {
        console.info(this.type)
        console.info(this.rowData)
        if (this.type == 'edit'){
          this.formList.find(item=>item.fieldName=="vin").disabled=true
          this.formList.forEach(x => {
            x.initialValue = this.rowData[x.fieldName];
          });
        }
      },
      createFormList() {
        return [
          {
            type: 'INPUT',
            label: 'VIN',
            fieldName: 'vin',
            placeholder: '请选择',
            initialValue: '',
            readonly: true,
            rules: [
              {required: true, trigger: 'change', message: '请选择VIN码'}
            ],
            unitRender: () => {
              return (
                <div>
                <el-button icon="el-icon-search" onClick={() => this.visbleVehicle = true}></el-button>
                </div>
              );
            },
          },
          // {
          //   type: 'INPUT',
          //   label: '车型',
          //   fieldName: 'model',
          //   disabled: true,
          // },
          {
            type: 'INPUT',
            label: 'SC码',
            fieldName: 'scCode',
            rules: [
              {required: true, trigger: 'change', message: '请输入SC码'}
            ],
          },
          {
            type: 'INPUT',
            label: 'SKC码',
            fieldName: 'skcCode',
            rules: [
              {required: true, trigger: 'change', message: '请选择SKC码'}
            ],
          }
        ]
      },
      formChangeHandle(val) {
        this.saveData = val;
      },
      /*选择车辆信息回调 */
      selectVehicleFun(arr) {
        console.info(arr)
        // this.modelId= arr[0].model;
        // this.formList.find(item => item.fieldName === 'model').initialValue= arr[0].modelName;
        this.formList.find(item => item.fieldName === 'vin').initialValue= arr[0].vin;
        this.visbleVehicle = false
      },
      async saveDialog() {
        console.info(this.saveData)
        console.info(this.rowData)
        const err = this.$refs.formPanel.SUBMIT_FORM();
        if (err === null) {
          return false
        }
        this.saveLoading = true
        console.info(this.saveData)
        if (this.type === 'edit') {
          this.saveData.id = this.rowData.id
          // this.saveData.modelId = this.rowData.modelId
          this.saveData.isAbnormal = this.rowData.isAbnormal
          console.info(this.rowData)
          console.info(this.saveData)
          const res = await updateSecurityCode(this.saveData);
          if (res.resultCode === 200 ) {
            this.closeDialog('refresh');
          }
        }else{
          const result = await getSecurityCodeByVin(this.saveData)
          console.info(result)
          if (result.data.length == 0){
          // this.saveData.modelId = this.modelId
          this.saveData.isAbnormal = 10041002
            const res = await setSecurityCode(this.saveData);
            if (res.resultCode === 200 ) {
              this.closeDialog('refresh');
              this.$message.success("保存成功!");
            }
          } else {
            this.$notify.error({title: '提示信息', message: '该车辆已维护防盗码！'});
          }
        }
        this.saveLoading = false
      },
      seacherFun(type){
        if (type == 1) {
          this.issearch = true
        } else {
          this.selectCentral = true
        }

      },
      closeDialog(param) {
        this.$emit('close', {visible: false, type: param});
      }
    }
  }
</script>

<style scoped>

</style>
